package com.example.administrator.yuejian_2.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.ClubIntroBean;
import com.example.administrator.yuejian_2.interservice.ClubService;
import com.example.administrator.yuejian_2.interservice.Const;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class ClubIntroFragment extends Fragment {
    private TextView clubinfo;
    private TextView clublocation;
    private TextView clubtel;
    private TextView clubtime;
    private TextView coachname;
    private TextView coachinfo;
    private ImageView coachpicture;
    private int club_id;
    private Context context;

    public ClubIntroFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_club_intro, container, false);

    }
    public void onViewCreated(View view, @Nullable Bundle saveInstanceState){
        Intent intent =getActivity().getIntent();
        club_id=intent.getIntExtra("club_id",0);

        clubinfo=view.findViewById(R.id.club_intro);
        clublocation=view.findViewById(R.id.club_local);
        clubtel=view.findViewById(R.id.club_tel);
        clubtime=view.findViewById(R.id.club_worktime);
        coachname=view.findViewById(R.id.coach_name);
        coachinfo=view.findViewById(R.id.coach_intro);
        coachpicture=view.findViewById(R.id.coach_pic);

        clubdetail();
    }

    private void clubdetail(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<ClubIntroBean> call = service.clubdetail(club_id);
        call.enqueue(new Callback<ClubIntroBean>() {
            @Override
            public void onResponse(Call<ClubIntroBean> call, Response<ClubIntroBean> response) {
               if (getActivity() != null){
                   clubinfo.setText(response.body().getClub_intro());
                   clublocation.setText(response.body().getClub_location());
                   clubtel.setText(response.body().getClub_tel());
                   clubtime.setText(response.body().getClub_worktime());
                   coachname.setText(response.body().getC_name());
                   coachinfo.setText(response.body().getC_intro());
                   String pic = response.body().getC_pic();
                   if (!pic.equals(""))
                       Picasso.with(context)
                               .load("http://10.0.2.2/YueJian16/Public/clubphoto/"+pic)
                               .into(coachpicture);

                }
            }

            @Override
            public void onFailure(Call<ClubIntroBean> call, Throwable t) {

            }
        });

    }


}


